/*******************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     main.c
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-01
  * @brief    This  file include main function.
 ********************************************************************************/

#include "sys.h"
#include "common.h"
#include "uart.h"
#include "debug.h"
#include "pit.h"
#include "data.h"
#include "function_drv.h"
#include "wdt_drv.h"
#include "wav_dac.h"
#include "bsp.h"
#include "tp_rtp.h"
#include "tp_ctp.h"
#include "sd_update.h"
#include "uart_update.h"
#include "levetop.h"
#include "tftlcd.h"
#include "dma.h"
#include "eflash_drv.h"
#include "iomacros.h"

uint8_t Sleep_flag = 0;
uint8_t Ye_Mian_num = 0;
uint8_t Lock_flag = 1;
uint8_t SW_BUTTON_flag=0;
uint8_t Dianji_flag=0;
uint8_t shanshuo_flag=0;
uint8_t pageSTART_flag;
uint8_t JiLiang_tiao_state;
uint8_t W_buff[WR_SIZE]={0};                                                
uint8_t buff_ALL[WR_SIZE]={1,0,0,0,0,0,0,0,0,1,1,1,5,0,0,0,0,0,0,0,0,21,0x0B,0xB8,0x17,0x70,0x23,0x28,0,0,0};//数组初始化
void flash_to_W_buff(void)
{
	for(uint8_t i=0;i<WR_SIZE;i++)
	{
		W_buff[i] = IO_READ8(flh_sAddr+i);
		printf("W_buff[%d]=0x%x \r\n",i,W_buff[i]);
	}
}


void save_count(void)
{
	uint8_t Cup_buff[2]={0};
	if(Mode1_count_ALL != ((uint32_t)IO_READ8(Cup_sAddr)<<8) + IO_READ8(Cup_sAddr+1))//第一杯记录
	{
		EFLASH_SetWritePermission();
		Cup_buff[0] = Mode1_count_ALL>>8;
		Cup_buff[1] = Mode1_count_ALL;
		EFLASH_Write(Cup_sAddr, &Cup_buff[0], 2);
		EFLASH_ClrWritePermission();		
	}
	if(Mode2_count_ALL != ((uint32_t)IO_READ8(Cup_sAddr+4)<<8) + IO_READ8(Cup_sAddr+5))//第二杯记录
	{
		EFLASH_SetWritePermission();
		Cup_buff[0] = Mode2_count_ALL>>8;
		Cup_buff[1] = Mode2_count_ALL;
		EFLASH_Write(Cup_sAddr+4, &Cup_buff[0], 2);
		EFLASH_ClrWritePermission();		
	}
	if(Mode3_count_ALL != ((uint32_t)IO_READ8(Cup_sAddr+8)<<8) + IO_READ8(Cup_sAddr+9))//第三杯记录
	{
		EFLASH_SetWritePermission();
		Cup_buff[0] = Mode3_count_ALL>>8;
		Cup_buff[1] = Mode3_count_ALL;
		EFLASH_Write(Cup_sAddr+8, &Cup_buff[0], 2);
		EFLASH_ClrWritePermission();		
	}
	if(Count_value_ALL/1000 != ((uint32_t)IO_READ8(Cup_sAddr+12)<<8) + IO_READ8(Cup_sAddr+13))//第四杯记录
	{
		EFLASH_SetWritePermission();
		Cup_buff[0] = (Count_value_ALL/1000)>>8;
		Cup_buff[1] = Count_value_ALL/1000;
		EFLASH_Write(Cup_sAddr+12, &Cup_buff[0], 2);
		EFLASH_ClrWritePermission();		
	}
}

void Count_value_to_Zero(void)
{
		//value1 = (((uint32_t)IO_READ8(AutoCupH_sAddr)<<8) + IO_READ8(AutoCupL_sAddr))*1000;//循环杯记录
		//value1 = value1 + (var[0x0231 * 2]<<8)+var[0x0231 * 2 + 1];
		Count_value = Count_value + (var[0x0231 * 2]<<8)+var[0x0231 * 2 + 1];
		Count_value_ALL = Count_value_ALL + (var[0x0231 * 2]<<8)+var[0x0231 * 2 + 1];
		var[0x0231 * 2] = 0;
		var[0x0231 * 2 + 1] = 0;	
		
		EFLASH_Init(g_sys_clk / 1000);
		EFLASH_SetWritePermission();
		W_buff[7] = (Count_value/1000)>>8;
		W_buff[8] = Count_value/1000;
		EFLASH_Write(flh_sAddr, &W_buff[0], WR_SIZE);
		EFLASH_ClrWritePermission();

		if(pic_id==33)	Display_page(33);
}

bool Sleep_deal_button_flag = FALSE;
void Chenk_button(void)
{
	uint16_t value = 0;
	uint16_t value1 = 0;
	if(!EPORT_ReadGpioData(EPORT_PIN32)&&SW_BUTTON_flag==0)
	{
		printf("AAAAAA55555AAAAAAA\r\n");
		if(Count_value_show_flag)
		{
			Count_value_show_flag = FALSE;
			Count_value_show_delay = 0;
			Count_value_to_Zero();
		}
		
		if(pic_id==17)//Backlight_flag == 0
		{
			var[0x2001*2+1] = 21&0xff;
			Sleep_deal_button_flag = TRUE;
			show_YeMian();
			printf("0x2001 -- %d\n",var[0x2001*2+1]);
		}
			
		SW_BUTTON_flag=1;
		if(pic_id==33)
		{
			var[0x0232 * 2 + 1]=1;
		}			
		else if(!Sleep_deal_button_flag)
		{
			if(pic_id==27)
			{
				Mode1_count++;
				Mode1_count_ALL++;
				value = ((uint32_t)IO_READ8(OneCupH_sAddr)<<8) + IO_READ8(OneCupL_sAddr) + 1;//第一杯记录
				EFLASH_Init(g_sys_clk / 1000);
				EFLASH_SetWritePermission();
				W_buff[1] = value>>8;
				W_buff[2] = value & 0XFF;
				EFLASH_Write(flh_sAddr, &W_buff[0], WR_SIZE);	
				EFLASH_ClrWritePermission();
				var[0x0202 * 2 + 1]=1;
				Display_page(28);                //IO口68
			}			
			else if(pic_id==29)	
			{
				Mode2_count++;
				Mode2_count_ALL++;
				value = ((uint32_t)IO_READ8(TwoCupH_sAddr)<<8) + IO_READ8(TwoCupL_sAddr) + 1;//第二杯记录
				EFLASH_Init(g_sys_clk / 1000);
				EFLASH_SetWritePermission();
				W_buff[3] = value>>8;
				W_buff[4] = value & 0XFF;
				EFLASH_Write(flh_sAddr, &W_buff[0], WR_SIZE);	
				EFLASH_ClrWritePermission();
				var[0x0212 * 2 + 1]=1;
				Display_page(30);             
			}
			else if(pic_id==31)	
			{
				Mode3_count++;
				Mode3_count_ALL++;
				value = ((uint32_t)IO_READ8(ThreeCupH_sAddr)<<8) + IO_READ8(ThreeCupL_sAddr) + 1;//第三杯记录
				EFLASH_Init(g_sys_clk / 1000);
				EFLASH_SetWritePermission();
				W_buff[5] = value>>8;
				W_buff[6] = value & 0XFF;
				EFLASH_Write(flh_sAddr, &W_buff[0], WR_SIZE);	
				EFLASH_ClrWritePermission();
				var[0x0222 * 2 + 1]=1;
				Display_page(32);           
			}			
			else if(pic_id==28)
			{
				if(var[0x0202 * 2 + 1]==1)	var[0x0202 * 2 + 1]=0;
				else if(var[0x0202 * 2 + 1]==0)	var[0x0202 * 2 + 1]=1;
			}			
			else if(pic_id==30)
			{
				if(var[0x0212 * 2 + 1]==1)	var[0x0212 * 2 + 1]=0;
				else if(var[0x0212 * 2 + 1]==0)	var[0x0212 * 2 + 1]=1;
			}
			else if(pic_id==32)
			{
				printf("var[0x0222 * 2 + 1]=%d \r\n",var[0x0222 * 2 + 1]);
				if(var[0x0222 * 2 + 1]==1)	var[0x0222 * 2 + 1]=0;
				else if(var[0x0222 * 2 + 1]==0)	var[0x0222 * 2 + 1]=1;
			}
		}
		else
		{
			
			PWM_WriteGpioData(PWM_PORT0, 0);
			var[0x0232 * 2 + 1] = 0;
		}	
	}
	else if(EPORT_ReadGpioData(EPORT_PIN32)&&SW_BUTTON_flag==1)
	{
		//printf("BBBBBBBBBB2222BBBBBB\r\n");
		SW_BUTTON_flag=0;

			if(pic_id==33)
			{
				PWM_WriteGpioData(PWM_PORT0, 0);
				var[0x0232 * 2 + 1] = 0;
				//DelayMS(1000);		
				Count_value_show_flag = TRUE;
			}	

		if((pic_id==27||pic_id==29||pic_id==31||pic_id==33))
			Sleep_deal_button_flag = FALSE;
	}
	
	if(Count_value_show_flag&&Count_value_show_delay==100)
	{
		Count_value_show_flag = FALSE;
		Count_value_show_delay = 0;	
		Count_value_to_Zero();
	}
}
void show_twinkle(void)
{
	if(twinkle_sum>=20)	
	{
		twinkle_sum=0;
		if(twinkle_mode==0)
		{
			twinkle_mode=1;
			Display_Count(0);
			if(pic_id==1||(pic_id>26&&pic_id<34))
			{
			Display_Icon(31, 207, 53, 0);
			}
		}
		else if(twinkle_mode==1)
		{
			twinkle_mode=0; 
			LT268_TFT_DrawSquare_Fill(0, 0, 0+240, 0+82, 0x0000);
		}
	}
}


void show_YeMian(void)
{
	if(Ye_Mian_num==0){Display_page(1);}
	else if(Ye_Mian_num==1){Display_page(27);}
	else if(Ye_Mian_num==2){Display_page(29);}
	else if(Ye_Mian_num==3){Display_page(31);}
	else if(Ye_Mian_num==4){Display_page(33);}
}



void PeripheralInit(void)
{
	/* system configuration��Obtain clock and other relevant parameters */
	Sys_Init();
	Enable_Interrupts;
	// SWD_Enable_Cmd(FALSE);    //SWD funtion disable
	WDT_control(0xf800, 0); // WDT funtion:0:close 1:open

	/* uart_update check */
#if (U_MCUFLASH_FLAG == 0)
	U_McuFlash_CheckBsp();
#endif
	
	/* uart debug */
#if UART_DEBUG
	UART_Debug_Init(SCI1, g_ips_clk, 115200); // TX1:uart printf  of  baudrate 115200
#endif
	
	/* communication port*/
#if (UARTBUS_OPTION == 0 || UARTBUS_OPTION == 1 || UARTBUS_OPTION == 2)
	UartTFT_SCI_Init(COMuart, 115200); // 268x default SCI3 as TFT communication
#endif
#if IIC_BUS
	// At present, the CTP and IIC slave use the same set of IO. If the IIC slave function is used, the touch function needs to be shielded first
	I2C_Slave_TEST(0x36); // Initialize IIC slave device address
	CTP_Init();
#endif

	/* exSPIFlash initialization */
	SSI1_STD_Init(SSI1);
	//IOCTRL->SPIM1_CONTROL_REG = 0x3f3f0013; // 12mA
	if (LT_W25N01GV_ReadID() == 0xefaa21){ Flash_Type = 2; W25N01GV_Init(); }
	else                                 { Flash_Type = 0; W25QXX_Init(); }
	
	//check_uid_entryption(16);//UID_2025_02_27

	/* get ui info */
	LT_ReadParam();

	/* communication port re-initialization */
#if (UARTBUS_OPTION == 0 || UARTBUS_OPTION == 1 || UARTBUS_OPTION == 2)
	UartTFT_SCI_Init(COMuart, gBaudrate);
#endif
#if (UARTBUS_OPTION == 2)
	Get_ModBus_CMD_info();
#endif
	
	/* mcu info printf */
	#if 1
	printf("/**************************************************/\n");
	printf(" date = %s,time = %s    \r\n", (char *)__DATE__, (char *)__TIME__);
	printf(" LT268C/D QFN68 Demo V1.1  II  \r\n");
	printf(" system clk = %dMHz         \r\n", g_sys_clk / 1000000);
	printf(" ips clk    = %dMHz         \r\n", g_ips_clk / 1000000);
	printf("/**************************************************/\r\n");
	#endif

	
	/* TFT init */
	LCD_TFT_Init();
//	LCD_XSIZE_TFT=320;  LCD_YSIZE_TFT=240; LCD_BL_Init(249); disp_V_Gray();  while(1);  		//RGB display(TFT test)

	/* pit init */
	PIT1_Init();

	/* RTC init */
	LT_RtcInit();

	/* touch init */
#if (LT_TOUCH_FLAG == 1)
	LT_TpInit();
#elif (LT_TOUCH_FLAG == 2)
	CTP_Init();
#endif
	uint8_t buff[6];
	buff[0] = 0x07;
	buff[1] = 0x10;
	
	EFLASH_Init(g_sys_clk / 1000);
	
	
	if(IO_READ8(JiYiOK_sAddr)!=0)//记忆是否OK，恢复默认记忆
	{
		printf("JiYiOK_sAddr = %d \r\n",JiYiOK_sAddr);
		uint8_t buff_JiYiOK[1]={0};
			
		EFLASH_SetWritePermission();
		EFLASH_Write(JiYiOK_sAddr, &buff_JiYiOK[0], 1);
		EFLASH_Write(flh_sAddr, &buff_ALL[0], WR_SIZE);	
		EFLASH_ClrWritePermission();
	}
	
	
	JiLiang_tiao_state = IO_READ8(JiLiang_tiao_sAddr);//每次选择剂量吗？
	if(JiLiang_tiao_state>2||JiLiang_tiao_state==0)
		JiLiang_tiao_state = 1;
	var[0x1101*2] = 0;
	var[0x1101*2+1] = JiLiang_tiao_state;
	
	Touch_flag = IO_READ8(Touch_sAddr);//触摸记录
	if(Touch_flag>1)
		Touch_flag = 1;
	printf("Touch_flag = %d \r\n",Touch_flag);
	buff[2] = 0x10;
	buff[3] = 0x48;
	buff[4] = 0x00;
	buff[5] = Touch_flag;
	LT_ReceiveCmd(buff);
	
	Button_flag = IO_READ8(Button_sAddr);//按键记录
	if(Button_flag>1)
		Button_flag = 0;
	printf("Button_flag = %d \r\n",Button_flag);
	buff[2] = 0x10;
	buff[3] = 0x49;
	buff[4] = 0x00;
	buff[5] = Button_flag;
	LT_ReceiveCmd(buff);
	
	Long_press_lock = IO_READ8(Lock_press_sAddr);//锁标记录
	if(Long_press_lock>1)
		Long_press_lock = 0;
	printf("Long_press_lock = %d \r\n",Long_press_lock);
	buff[2] = 0x0A;
	buff[3] = 0x08;
	buff[4] = 0x00;
	buff[5] = Long_press_lock;
	LT_ReceiveCmd(buff);
	
	Language_state = IO_READ8(Language_sAddr);//语言
	buff[2] = 0x19;
	buff[3] = 0x00;
	buff[4] = 0x00;
	if(Language_state>12)
		buff[5] = 1;
	else
		buff[5] = Language_state;
	printf("language = %d \r\n",buff[5]);
	LT_ReceiveCmd(buff);
	
	if(IO_READ8(Ye_Mian_sAddr)>4)
		Ye_Mian_num = 0;
	else
		Ye_Mian_num = IO_READ8(Ye_Mian_sAddr);
	printf("Ye_Mian_num = %d \r\n",Ye_Mian_num);
	
	if(IO_READ8(pageSTART_sAddr)>1)
		pageSTART_flag = 0;
	else
		pageSTART_flag = IO_READ8(pageSTART_sAddr);
	printf("pageSTART_sAddr = %d \r\n",pageSTART_flag);
	

	Mode1_count_ALL = ((uint32_t)IO_READ8(Cup_sAddr)<<8) + IO_READ8(Cup_sAddr+1);//第一杯记录
	if(Mode1_count_ALL>60000)
		Mode1_count_ALL = 0;
	Mode2_count_ALL = ((uint32_t)IO_READ8(Cup_sAddr+4)<<8) + IO_READ8(Cup_sAddr+5);//第二杯记录
	if(Mode2_count_ALL>60000)
		Mode2_count_ALL = 0;
	Mode3_count_ALL = ((uint32_t)IO_READ8(Cup_sAddr+8)<<8) + IO_READ8(Cup_sAddr+9);//第三杯记录
	if(Mode3_count_ALL>60000)
		Mode3_count_ALL = 0;
	Count_value_ALL = (((uint32_t)IO_READ8(Cup_sAddr+12)<<8) + IO_READ8(Cup_sAddr+13))*1000;//循环杯记录
	if(Count_value_ALL>60000000)
		Count_value_ALL = 0;
	Count_value = (((uint32_t)IO_READ8(AutoCupH_sAddr)<<8) + IO_READ8(AutoCupL_sAddr))*1000;//循环杯记录
	if(Count_value>60000000)
		Count_value = 0;

	//Get_Gif_addr();
	if(pageSTART_flag==1)
	{
		Display_page(0);
		buff[5] = 1;
	}
	else
	{
		show_YeMian();
		buff[5] = 0;
	}
		buff[2] = 0x02;
		buff[3] = 0x75;
		buff[4] = 0x00;
		LT_ReceiveCmd(buff);

//	Display_page(1);
	
	EPORT_ConfigGpio(EPORT_PIN32, GPIO_INPUT);	//检测脚
	
	PWM_ConfigGpio(PWM_PORT0, GPIO_OUTPUT, PWM_PULLUP_DIS);//电机
	PWM_WriteGpioData(PWM_PORT0, 0);
	printf("main--PWM_WriteGpioData(PWM_PORT0, 0);\r\n");
	
	/* display UI start page */
	//if (Start_page > addr_index[7])
	//	Display_page(0);
	//else
	//	Display_page(Start_page);

	/* backlight */
//	LCD_BL_Init(gDutyBuf[var[VAR_BL * 2 + 1]]);
	//DelayMS(1000);


	
	
	/* touch buzzer */
#if LT_TOUCH_BUZZER
	if (buzzer == 1)
		touch_buzzer_init();
#endif

	/* encoder */
#if LT_ENCODER
	Encoder_Init();
#endif

	/* SDcard */
#if LT_SD_UPDATE
	SD_IN_Enable();
	SD_ud_flag = SD_detection();
#endif
}

int32_t main(void)
{
	uint8_t Cup_buff[2]={0};
	uint32_t Baoyang_temp;
	uint8_t Buff[6]={2,1,4,3,6,5};
	/* uart IAP */
#if (U_MCUFLASH_FLAG == 1) 
	NVIC_SetVectorTable(0x08043000, 0);
#else
	NVIC_SetVectorTable(0x08008000, 0);
#endif

	PeripheralInit();
	flash_to_W_buff();	//读flash回W_buff
	//Get_Gif_addr();	
	
	if(pageSTART_flag==0)
			show_YeMian();
	
#if 0
			
#else
	if(IO_READ8(OneCupTimeH_sAddr)>=0x80)
	{
		var[0x0201*2] = 3000>>8;
		var[0x0201*2+1] = 3000&0xff;
	}
	else
	{
		var[0x0201*2] = IO_READ8(OneCupTimeH_sAddr);
		var[0x0201*2+1] = IO_READ8(OneCupTimeH_sAddr+1);
	}
	
	if(IO_READ8(TwoCupTimeH_sAddr)>=0x80)
	{
		var[0x0211*2] = 6000>>8;
		var[0x0211*2+1] = 6000&0xff;	
	}
	else
	{
		var[0x0211*2] = IO_READ8(TwoCupTimeH_sAddr);
		var[0x0211*2+1] = IO_READ8(TwoCupTimeH_sAddr+1);
	}
	
	if(IO_READ8(ThreeCupTimeH_sAddr)>=0x80)
	{
		var[0x0221*2] = 9000>>8;
		var[0x0221*2+1] = 9000&0xff;	
	}
	else
	{
		var[0x0221*2] = IO_READ8(ThreeCupTimeH_sAddr);
		var[0x0221*2+1] = IO_READ8(ThreeCupTimeH_sAddr+1);
	}
	
	if(IO_READ8(Sleep_Time_sAddr)>60||IO_READ8(Sleep_Time_sAddr)==0)
	{
		var[0x2010*2] = buff_ALL[32]>>8;       
		var[0x2010*2+1] = buff_ALL[32]&0xff;
	}
	else
	{
		var[0x2010*2] = IO_READ8(Sleep_Time_sAddr)>>8;
		var[0x2010*2+1] = IO_READ8(Sleep_Time_sAddr)&0xff;
	}
	
	Sleep_flag = IO_READ8(Sleep_sAddr);
	if(Sleep_flag>1)
	{
	var[0x200e*2+1] = 0;
	var[0x200f*2+1] = 0;
	}
	else
	{
	var[0x200e*2+1] = Sleep_flag;
	var[0x200f*2+1] = 0;
	}
	printf("0x2010 -- %d\n",var[0x2010*2+1]);
	printf("0x200E -- %d\n",var[0x200E*2+1]);
	printf("0x200F -- %d\n",var[0x200F*2+1]);
	printf("0x2001 -- %d\n",var[0x2001*2+1]);
	
  //printf("0x200F -- %d\n",var[0x200F*2+1]);

    printf("0x1025 = %d ,0x1026 = %d,0x1027 = %d,0x1028 = %d \r\n",var[0x1025*2+1],var[0x1026*2+1],var[0x1027*2+1],var[0x1028*2+1]);
#endif

	if(IO_READ8(Lock_press_sAddr)>1)
		Long_press_lock = 0;
	else
		Long_press_lock = IO_READ8(Lock_press_sAddr);
	
	Mode_I_Sum = (var[0x0201 * 2] << 8) | var[0x0201 * 2 + 1];
	Mode_II_Sum = (var[0x0211 * 2] << 8) | var[0x0211 * 2 + 1];
	Mode_III_Sum = (var[0x0221 * 2] << 8) | var[0x0221 * 2 + 1];	

	
	Backlight_state = IO_READ8(Backlight_sAddr);//背光
	if(Backlight_state>21||Backlight_state==0)
		var[VAR_BL*2+1] = 21;	
	else
		var[VAR_BL*2+1] = Backlight_state;	

	printf("Backlight_state = %d \r\n",var[0x2001*2+1]);
	LCD_BL_Init(gDutyBuf[var[VAR_BL * 2 + 1]]);
	
	
	while (1)
	{

		if(shanshuo_flag==0 && (((pic_id==28) && var[0x0202 * 2 + 1]==0)||((pic_id==30) && var[0x0212 * 2 + 1]==0)||((pic_id==32) && var[0x0222 * 2 + 1]==0)))
		{
			shanshuo_flag=1;twinkle_flag=1;twinkle_sum=0;
		}			
		else if(shanshuo_flag==1 && (((pic_id==28) && var[0x0202 * 2 + 1]==1)||((pic_id==30) && var[0x0212 * 2 + 1]==1)
		||((pic_id==32) && var[0x0222 * 2 + 1]==1)||(pic_id!=28&&pic_id!=30&&pic_id!=32)))
		{
			shanshuo_flag=0;twinkle_flag=0;
			if(pic_id==1||(pic_id>26&&pic_id<34))
			{
			Display_Icon(31, 207, 53, 0);
			}
		}
		
		if(twinkle_flag)	show_twinkle();			//计时停止闪烁
		if(Button_flag == 1)	Chenk_button();		//扫描按键
		if(Dianji_flag==0 && (var[0x0202 * 2 + 1]==1||var[0x0212 * 2 + 1]==1||var[0x0222 * 2 + 1]==1||var[0x0232 * 2 + 1]==1))			//电机启动和停止
		{
			Backlight_count=0;
			Dianji_flag=1;
			PWM_WriteGpioData(PWM_PORT0, 1);
		}
		else if(Dianji_flag==1 && var[0x0202 * 2 + 1]==0 && var[0x0212 * 2 + 1]==0 && var[0x0222 * 2 + 1]==0 && var[0x0232 * 2 + 1]==0)
		{
			Backlight_count=0;
			Dianji_flag=0;
			PWM_WriteGpioData(PWM_PORT0, 0);
		}

		if(touch_nolock_time>600)
		{
			if(pic_id==3)
			{
			EFLASH_Init(g_sys_clk / 1000);
			Cup_buff[0] = 0;
			Cup_buff[1] = 0;
			EFLASH_SetWritePermission();
			EFLASH_Write(Cup_sAddr, &Cup_buff[0], 2); 
			EFLASH_Write(Cup_sAddr+4, &Cup_buff[0], 2);
			EFLASH_Write(Cup_sAddr+8, &Cup_buff[0], 2);
			EFLASH_Write(Cup_sAddr+12, &Cup_buff[0], 2);
			EFLASH_ClrWritePermission();		
			Mode1_count_ALL=0;Mode2_count_ALL=0;Mode3_count_ALL=0;Count_value_ALL=0;
			Reset_flag=1;
			Display_page(3);
			}
			else if(pic_id==5||pic_id==7)
			{
			EFLASH_Init(g_sys_clk / 1000);
			EFLASH_SetWritePermission();
			W_buff[13] = 0;	
			EFLASH_Write(flh_sAddr, &W_buff[0], WR_SIZE);	
			EFLASH_ClrWritePermission();
			Display_page(5);
			DelayMS(1000);
			}
			touch_nolock_flag=0;touch_nolock_time=0;			
		}
#if !IIC_BUS
#if (UARTBUS_OPTION == 0)
		Main_From_UartRxBuff();
		if (gUsartRx.Flag)
			LT_ReceiveCmd(gUsartRx.Buf);
#elif (UARTBUS_OPTION == 1)
		LT_ModBus_REG_Cmd();
#elif (UARTBUS_OPTION == 2)
		if (Sum_ModbusTX)
			Uart_cmd_Send();
		LT_ModBus_REG_Cmd();
#endif
#endif
#if IIC_BUS
		LT_IIC_REG_Cmd(); // handle instruction function of writing register
#endif
		show_Count();	 // time counter
		if(delay_flag)
		{
			delay_flag=0;
			DelayMS(1000);
		}
		if (page_refreshing > 30)
		//printf("13333333\r\n",Dianji_flag);
		{
			page_refreshing = 0;
			Get_date_variable_Id_Regular_update(pic_id);	
			save_count();
		}
		if(Count_reg_num > 0)
		//printf("177777777\r\n",Dianji_flag);
		{ 			
			Count_reg_num--;
			reg_operation(Count_reg[Count_reg_num]);
		}
		
		if (gWavFlag == 1 || gWavType == 1)
			LT_PlayWav_DAC(); // WAV

#if (LT_TOUCH_FLAG == 1 || LT_TOUCH_FLAG == 2)
		gTpInfo.scan(); // Touch Point
#endif
		
		if (var[VAR_BLAUTO * 2 + 1] == 1)
			BackLight_control(); // backlight control
		
		show_gif();		 // gif
		show_Clock();	 // analog clock
		show_RTC();		 // digital clock
		scroll_text();	 // text scroll
		Display_Curve(); // Curve

#if LT_ENCODER
		if (gEncoderFlag)
			encoder(); // Encoder
#endif
		// if(Ges_sc_flag) slide_screen();       //unsupported
		if (Gesture_flag)
			Gesture_touch(); // sliding gesture

		Basic_touch();			// basic touch control
		Adj_touch();			// variable adjustment
		Progress_bar_sliding(); // sliding progress bar
		data_input();			// data input
		slideMune();			// slide menu
		RingSld_touch();		// ring progress bar with touch
		Ascii_input();			// ASCII keyboard
		GBK_input();			  // GBK keyboard
		VarKey_touch();			// multivariable adjustment
		pop_up_touch();			// pop-up window

#if LT_SD_UPDATE //---SDcard---
		if (SD_ud_flag)
		{
			if (!SD_detection())
				Sd_To_Flash(); // SD_pin detects a low level when an SD card is inserted.
		}
		else
		{
			if (SD_detection())
				SD_ud_flag = 1;
		}
#endif
		

	}
}
